package com.meme.editor.level;

import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.ListIterator;

import com.meme.io.editor.FileCache;

import com.meme.editor.model.sprite2d.Sprite2dFile;
import com.meme.editor.model.sprite2d.AnimatedModuleGroup;
import com.meme.editor.model.sprite2d.AnimatedModule;
import com.meme.editor.property.PropertyFactory;
import com.meme.editor.property.value.EntityLinkPropertyValue;
import com.meme.editor.Box2d;
import com.meme.editor.PropertyValueModification;
import com.meme.editor.PropertyValueModificationList;

import com.meme.editor.level.define.DefineFile;

import com.meme.util.FileUtil;



public class Sprite2dAppearance extends Appearance 
{
	public static final String ParamName_Sprite2dFile = "sprite2d_file";
	public static final String ParamName_ModelName = "model_name";
	public static final String ParamName_AnimaitonName = "animation_name";
	
	
	final static String GroupName_SpriteGroup = "Sprite Properties";
	final static String PropertyName_Animation        = "Animation";
	

	Sprite2dFile mSprite2dFile;
	AnimatedModuleGroup mAnimatedModuleGroup;
	AnimatedModule mAnimatedModule;
	
	public Sprite2dAppearance ()
	{
		super ();
		createPublicProperty (GroupName_SpriteGroup, PropertyName_Animation, PropertyFactory.ValueType_EntityRef );
	}
	
	@Override
	protected void _handleModifyingPropertyValues (PropertyValueModificationList modications)
	{			
		super._handleModifyingPropertyValues (modications);
		
		ListIterator<PropertyValueModification> iter = modications.listIterator();
		while ( iter.hasNext() )
		{
			PropertyValueModification mod = iter.next();
			
			if ( mod.mGroupName.equals (GroupName_SpriteGroup) )
			{
				if ( mod.mPropertyName.equals (PropertyName_Animation) )
				{
					setAnimation ( (AnimatedModule) (mod.mNewValue) );
					iter.remove();
				}
					
			}
		}
	}
	
	public void setAnimation (AnimatedModule animation)
	{
		mAnimatedModule = animation;
		getPublicProperty (GroupName_SpriteGroup, PropertyName_Animation).modifyValue (mAnimatedModule);
	}
	
	public AnimatedModule getAniamation ()
	{
		return mAnimatedModule;
	}
	
	
	@Override
	public void setParameter (String paramName, String paramValue)
	{
		if (paramName.equals(ParamName_Sprite2dFile))
		{
			DefineFile defineFile = ( (LevelFile) getHost () ).getDefineFile();
			
			String filepath = FileUtil.getFullPathFilename(paramValue, defineFile.getHostFilename());
			
			mSprite2dFile = FileCache.getSprite2dFile (filepath);
		}
		else if (paramName.equals(ParamName_ModelName))
		{
			if (mSprite2dFile != null)
			{
				mAnimatedModuleGroup = (AnimatedModuleGroup) (mSprite2dFile.parseNodePath(paramValue));
				
				getPublicProperty (GroupName_SpriteGroup, PropertyName_Animation)
					   .setParameter (EntityLinkPropertyValue.Param_NodePath, "$Sprite2dFile/" + paramValue + "/$_children");
			}
		}
		else if (paramName.equals(ParamName_AnimaitonName))
		{
			if (mAnimatedModuleGroup != null)
			{
				setAnimation ((AnimatedModule) (mAnimatedModuleGroup.getChild (paramValue)));
			}
		}
	}
	
	@Override
	public Object getProperty (String propertyName)
	{
		if (propertyName.equals("Sprite2dFile"))
		{
			return mSprite2dFile;
		}
		
		return super.getProperty (propertyName);
	}
	
//	@Override
//	public void show (Graphics2D g, int posX, int posY, boolean flipX, boolean flipY, int scale, int flag)
//	{
//		if (mAnimatedModule != null)
//			mAnimatedModule.render (g, posX, posY, flipX, flipY, scale, flag);
//	}
	
	@Override
	public void show (Graphics2D g, int flag)
	{
		if (mAnimatedModule != null)
			mAnimatedModule.render (g, 0, 0, false, false, 1, flag);
	}
	
	@Override
	public void getBoundingBox (Box2d box2d)
	{
		if (mAnimatedModule != null)
		{
			box2d.set (
					mAnimatedModule.getMinX(),
					mAnimatedModule.getMinY(),
					mAnimatedModule.getMinX() + mAnimatedModule.getWidth(),
					mAnimatedModule.getMinY() + mAnimatedModule.getHeight()
					);
		}
		else
			box2d.reset ();
		
//		Actor actor = (Actor) getParent ();
//		box2d.adjust (actor.isFlipX (), actor.isFlipY ());
//		box2d.shift(actor.getPosX(), actor.getPosY());
	}
}
